package com.clh.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.clh.common.PageInfo;
import com.clh.dto.Result;
import com.clh.entity.Complaint;
import com.clh.entity.Owner;
import com.clh.service.ComplaintService;
import com.clh.service.impl.ComplaintServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Servlet implementation class ComplaintServlet
 */
/**
 * 
 * @Description: 投诉界面控制
 * @author 陈志艺
 * @date 2022年5月26日 下午3:46:26
 * @version v1.0
 */
@WebServlet("/ComplaintServlet")
public class ComplaintServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ComplaintService comService = new ComplaintServiceImpl();

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String op = request.getParameter("op");
		if ("page".equals(op)) {// 执行查询
			this.doComPageQuery(request, response);
		} else if ("delete".equals(op)) {
			this.dodelete(request, response);
		} else if ("update".equals(op)) {
			this.doUpdate(request, response);
		} else if ("add".equals(op)) {
			this.doAddCom(request, response);
		} else if ("addInUser".equals(op)) {
			this.doAddInUser(request, response);
		}else if("findByComNo".equals(op)) {
			this.dofindByNo(request, response);
		}else if("count".equals(op)) {//统计每户投诉次数
			this.doCount(request,response);
		}
	}

	/**
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void doCount(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 统一的响应对象格式
		Result result = new Result();
		
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		//调用业务获取各户投诉次数情况
		List<Owner> list = comService.countForComplaint(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
		if(list==null) {
			result.ok(true).setMessage("无人投诉！无可能！");
		}else {
			result.ok(list);
		}
		
		// 响应
		response.setContentType("application/json;charset=utf-8");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		// 创建响应并将数据返回给浏览器
		response.getWriter().write(json);
	}

	/**
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void doAddInUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 统一的响应对象格式
		Result result = new Result();
		// 接受请求参数
		String roomNo = request.getParameter("roomNo");
		String comContent = request.getParameter("comcontent");

		Complaint com = new Complaint();
		com.setComContent(comContent);
		com.setRoomNo(roomNo);
		com.setComStatus(01);
		// 调用业务层，执行添加任务
		boolean f = comService.addComplaint(com);
		if (f) {
			result.ok(f);
		} else {
			result.fail(0, "新增投诉信息失败");
		}

		// 响应
		response.setContentType("application/json;charset=utf-8");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		// 创建响应并将数据返回给浏览器
		response.getWriter().write(json);
	}

	/**
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void doAddCom(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 统一的响应对象格式
		Result result = new Result();
		// 接受请求参数
		String roomNo = request.getParameter("roomNo");
		String comTime = request.getParameter("comTime");
		String comContent = request.getParameter("comcontent");
		String comStatus = request.getParameter("comStatus");
		String comHandler = request.getParameter("comHandler");
		// 构建对象
		Complaint com = new Complaint();
		com.setComContent(comContent);
		com.setComHandler(comHandler);
		com.setComStatus(comStatus == null ? 0 : 1);// 复选框
		com.setComTime(comTime);
		com.setRoomNo(roomNo);

		boolean f = comService.addComplaint(com);
		if (f) {
			result.ok(f);
		} else {
			result.fail(0, "投诉信息添加失败");
		}

		// 调用业务层
		// ...
		// 响应
		response.setContentType("application/json;charset=utf-8");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		// 创建响应并将数据返回给浏览器
		response.getWriter().write(json);
	}

	/**
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void doUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 统一的响应对象格式
		Result result = new Result();

		// 接受请求参数
		String comNo = request.getParameter("comNo");// 修改条件
		String roomNo = request.getParameter("roomNo");
		String comTime = request.getParameter("comTime");
		String comContent = request.getParameter("comcontent");
		String comStatus = request.getParameter("comStatus");
		String comHandler = request.getParameter("comHandler");
		// 构建商品对象
		Complaint com = new Complaint();
		com.setComNo(comNo);
		com.setRoomNo(roomNo);
		com.setComTime(comTime);
		com.setComContent(comContent);
		com.setComStatus(comStatus == null ? 1 : 0);// 复选框
		com.setComHandler(comHandler);
		
		
		
		boolean f = comService.modifyComplaint(com);
		if (f) {
			result.ok(f);
		} else {
			result.fail(0, "修改投诉信息失败");
		}
		// 响应
		response.setContentType("application/json;charset=utf-8");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		// 创建响应并将数据返回给浏览器
		response.getWriter().write(json);
	}

	/**
	 * @param request
	 * @param response
	 */
	private void dodelete(HttpServletRequest request, HttpServletResponse response) {

		// 统一的响应对象格式
		Result result = new Result();
		try {
			String comNo = request.getParameter("comNo");
			if (comNo == null || "".equals(comNo)) {
				result.fail(400, "参数错误");
			} else {
				int n = comService.removeComplaint(comNo);
				if (n > 0) {
					result.ok(true).setMessage("删除成功");
				} else {
					result.fail(0, "删除失败，可能原因是它不存在，或者被关联引用！");
				}
			}
//					// 设置相应的内容类型
			response.setContentType("application/json;charset=utf-8");
			// 利用第三方库jackson转换
			ObjectMapper objectMapper = new ObjectMapper();
			String json = objectMapper.writeValueAsString(result);
			// 创建响应并将数据返回给浏览器
			response.getWriter().write(json);

		} catch (Exception e) {
			result.fail(500, "服务器出错了");
		}
	}

	/**
	 * 用于修改
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void dofindByNo(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 统一的响应对象格式
		Result result = new Result();
		// 接受请求参数
		String comNo = request.getParameter("comNo");
		// 校验参数
		//System.out.println(comNo);
		if (comNo == null || "".equals(comNo.trim())) {
			//System.out.println("空的");
			result.fail(0, "参数错误");
		} else {
			// 调用业务层
			Complaint com=comService.getComById(comNo);
			if (com != null) {
				result.ok(com);
			} else {
				result.fail(0, "没有相关数据");
			}
		}
		// 响应
		response.setContentType("application/json;charset=utf-8");
		// 利用第三方库jackson转换
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		// 创建响应并将数据返回给浏览器
		response.getWriter().write(json);
		
		
	}

	/**
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void doComPageQuery(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 统一的响应对象格式
		Result result = new Result();

		// 接受请求参数
		String start = request.getParameter("start");
		String end = request.getParameter("end");
		String comStatus = request.getParameter("comStatus");
		String roomNo = request.getParameter("roomNo");
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		// 对请求参数进行校验
		if ((pageNum == null || "".equals(pageNum) || Integer.parseInt(pageNum) < 0)
				|| (pageSize == null || "".equals(pageSize) || Integer.parseInt(pageSize) < 0)) {
			result.fail(0, "参数错误");
		} else {
			// 调用业务层
			PageInfo<Complaint> pageInfo = comService.getComplaintListByCondtion(start, end, comStatus, roomNo,
					Integer.parseInt(pageNum), Integer.parseInt(pageSize));
			if (!pageInfo.getList().isEmpty()) {
				result.ok(pageInfo);
			} else {
				result.fail(0, "没有相关数据");
			}
		}
		// 响应
		response.setContentType("application/json;charset=utf-8");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		// 创建响应并将数据返回给浏览器
		response.getWriter().write(json);

	}

}
